'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 商品列表
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var resourceConf = require('../../../../config/resourceConf');
var dataApi = require('../../../../libs/dataApi');
var vipShopConf = require('../../../../config/vipShopConf');
var PlayerManager = require('../../../../libs/playerManager');
var Code = require('../../../../consts/code');
var unionGroupModel = require('../../../../models/unionGroupModel.js');
var constantUtil = require('../../../../util/constantUtil.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var role_model;
    var union_id = 0;
    var union_group_model;
    var unionTech_list = [];
    var list = [];
    var donated_list = [];
    var donated_time = 0;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'friend', 'task', 'achieve', 'daily_task', 'unionTask', 'accrued'],
                    cb
                );
            },
            function (cb) {
                role_model = player_manager.role;
                union_id = role_model.getUnionId();

                if (!union_id) {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }
                unionGroupModel.getById(union_id, cb);
            },
            function (model, cb) {
                union_group_model = model;
                var union_lv = union_group_model.lv;
                var open_lv = constantUtil.value('union_need_junior_tech_lvl');
                unionTech_list = union_group_model.union_tech_list;

                var union_task_model = player_manager.unionTask;
                donated_time = union_task_model.donated_time;
                donated_list = union_task_model.donated_list;
                _.some(unionTech_list, function (item) {
                    var item1 = {
                        tech_id: item.tech_id,
                        type: item.type,
                        star: item.star,
                        lvl: item.lvl,
                        exp: item.exp,
                        open_state: 1
                    };
                    list.push(item1);
                });

                var isVaild = true;
                _.some(list, function (list_item) {
                    if (list_item.type == 1 && list_item.lvl < open_lv) {
                        isVaild = false;
                    }
                });

                if (!isVaild) {
                    _.each(list, function (item1) {
                        if (item1.type == 2) {
                            item1.open_state = 0;
                        }
                    });
                }
                cb();
            },
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    unionTech_list: list,
                    donated_list: donated_list,
                    donated_time: donated_time
                }
            });
        }
    );
};
